using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using Conusic.Properties;

namespace Conusic
{
    public sealed class Config
    {
        private const int DefaultOtherSitesPoolSize = 5;
        private const int DefaultBitratePoolSize = 5;
        private const int DefaultDownloadPoolSize = 5;

        #region Singletone declaration

        private static readonly Config instance = new Config();

        private Config()
        {
        }

        public static Config Instance
        {
            get { return instance; }
        }

        #endregion

        private IWebProxy proxy;
        //vkontakte access mutex
        //safe from "more that one page per second" error
        public readonly ThreadTube VkontakteAccessTube = new ThreadTube(VkontakteTimeout);
        public readonly Semaphore DownloadPool = new Semaphore(DefaultDownloadPoolSize, DefaultDownloadPoolSize);
        public readonly Semaphore BitratePool = new Semaphore(DefaultBitratePoolSize, DefaultBitratePoolSize);
        public readonly Semaphore OtherSitesPool = new Semaphore(DefaultOtherSitesPoolSize, DefaultOtherSitesPoolSize);

        public IWebProxy Proxy
        {
            set
            {
                if (value == null) throw new ArgumentNullException("value");
                proxy = value;
            }
            get { return proxy; }
        }

        public enum ProxyType
        {
            Automatic = 0,
            Direct = 1,
            Manual = 2
        }

        public void UpdateProxyParams(ProxyType proxyType, bool useAuth, string address = null, int port = 0,
                                      string user = null, string password = null)
        {
            switch (proxyType)
            {
                case ProxyType.Automatic:
                    WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
                    break;
                case ProxyType.Direct:
                    WebRequest.DefaultWebProxy = new WebProxy();
                    break;
                case ProxyType.Manual:
                    WebRequest.DefaultWebProxy = new WebProxy(address, port);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("proxyType");
            }
            if (useAuth)
            {
                WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(user, password);
            }
            Proxy = WebRequest.DefaultWebProxy;
        }

        public void SetVkontakteCookieForWebRequest(WebRequest request)
        {
            string sid = Settings.Default.SID;
            request.Headers.Add("Cookie", String.Format("remixsid={0}", sid));
        }

        public void SetProxyConfiguration(HttpWebRequest request)
        {
            request.UserAgent =
                "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
        }

        #region Settings

        private readonly Dictionary<string, string> parametersString = new Dictionary<string, string>();
        private readonly Dictionary<string, int> parametersInt = new Dictionary<string, int>();
        private const int VkontakteTimeout = 1600;

        #endregion
    }
}
